package com.meishij.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.meishij.entry.MallSellEntry;

import java.util.List;

/**
 * Created by 肖荣 on 2016/10/21.
 */

public class MallSellImagesViewPagerAdapter extends PagerAdapter {
    private List<MallSellEntry.ObjBean.ImagesBean> list;
    private Context context;
    private Pools.Pool<SimpleDraweeView> pool=new Pools.SimplePool<>(4);
    public MallSellImagesViewPagerAdapter(List<MallSellEntry.ObjBean.ImagesBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        SimpleDraweeView simpleDraweeView = pool.acquire();
        if(simpleDraweeView ==null){
            simpleDraweeView = new SimpleDraweeView(context);
        }
        simpleDraweeView.setImageURI(list.get(position).getSmall());
        simpleDraweeView.setAdjustViewBounds(true);
        LinearLayout.LayoutParams layoutParams =  new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        simpleDraweeView.setLayoutParams(layoutParams);
        simpleDraweeView.setAspectRatio(1.0f);
        container.addView(simpleDraweeView);
        return simpleDraweeView;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(((View) object));
    }
}
